#include "settingfunc.h"
#include "ui_settingfunc.h"

SettingFunc::SettingFunc(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingFunc)
{
    ui->setupUi(this);
    m_loadQss.loadStyleSheet(this, ":/qss/Settings.qss");
    this->setWindowTitle("设置");
    this->setWindowIcon(QIcon(":/img/settings.png"));

    loadVideoMode();
    m_configPanel_1 = new VideoConfigWidget(m_videoMode[0]);
    m_configPanel_2 = new VideoConfigWidget(m_videoMode[1]);
    m_configPanel_3 = new VideoConfigWidget(m_videoMode[2]);
    m_configPanel_4 = new VideoConfigWidget(m_videoMode[3]);
    m_localSetting = new LocalSetting();
    m_saveSettings = new SaveSettings();

    m_configPanels.push_back(m_configPanel_1);
    m_configPanels.push_back(m_configPanel_2);
    m_configPanels.push_back(m_configPanel_3);
    m_configPanels.push_back(m_configPanel_4);

    connect(m_configPanel_1, &VideoConfigWidget::sigVideoChanged,
            [=](WINDOW_CONFIG m_sendConfig){
                emit sigVideoChanged(0, m_sendConfig);
            });
    connect(m_configPanel_2, &VideoConfigWidget::sigVideoChanged,
            [=](WINDOW_CONFIG m_sendConfig){
                emit sigVideoChanged(1, m_sendConfig);
            });
    connect(m_configPanel_3, &VideoConfigWidget::sigVideoChanged,
            [=](WINDOW_CONFIG m_sendConfig){
                emit sigVideoChanged(2, m_sendConfig);
            });
    connect(m_configPanel_4, &VideoConfigWidget::sigVideoChanged,
            [=](WINDOW_CONFIG m_sendConfig){
                emit sigVideoChanged(3, m_sendConfig);
            });

    for(int i = 0; i < m_configPanels.size(); i++) {
        connect(m_configPanels[i], &VideoConfigWidget::sigClick, [=](int videoNum){
            emit sigSetClick(i, videoNum);
        });
        connect(m_configPanels[i], &VideoConfigWidget::sigChangedDisplayMode, [=](int videoMode){
            emit sigChangeDisplayMode(i, videoMode);
        });
    }

    connect(m_saveSettings, &SaveSettings::sigSettingsData, this, &SettingFunc::sigSettingsData);

    m_saveSettings->sendSigToSaveData();  //发送配置到后端 savedata/maincontrol.cpp,连接后发送

    //根据显示屏的数量，显示n个设置界面
    QList<QScreen*> screens = QGuiApplication::screens();
    for(int i = 0; i < screens.size(); i++) {
        ui->tabWidget->addTab(m_configPanels[i], QString("显示器%1设置").arg(screens[i]->name()));
    }

    //ui->tabWidget->addTab(m_localSetting, QString("本地设备设置"));
    ui->tabWidget->addTab(m_saveSettings, QString("存储参数设置"));

    QVBoxLayout *layout = new QVBoxLayout(this);  //为了让tabwidget随父窗口变大缩小
    layout->addWidget(ui->tabWidget);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);
}

SettingFunc::~SettingFunc()
{
    delete ui;

    saveVideoMode();
    delete m_configPanel_1;
    delete m_configPanel_2;
    delete m_configPanel_3;
    delete m_configPanel_4;
    delete m_localSetting;
    delete m_saveSettings;
}

void SettingFunc::configSettings(const QVector<WINDOW_CONFIG> configs)
{
    //配置16 * 5个控件的内容
    for(int i = 0; i < 4; i++) {
        WINDOW_CONFIG config_1 = configs[0 + 4 * i];
        config_1.videoNum = 0;
        m_configPanels[i]->resourcesNameLine_1->setText(config_1.resourcesName);
        m_configPanels[i]->ipLine_1->setText(config_1.ip);
        m_configPanels[i]->portSpinBox_1->setValue(config_1.port);
        m_configPanels[i]->m_netCardComb_1->setCurrentText(config_1.netCard);
        emit sigVideoChanged(i, config_1);

        WINDOW_CONFIG config_2 = configs[1 + 4 * i];
        config_1.videoNum = 1;
        m_configPanels[i]->resourcesNameLine_2->setText(config_2.resourcesName);
        m_configPanels[i]->ipLine_2->setText(config_2.ip);
        m_configPanels[i]->portSpinBox_2->setValue(config_2.port);
        m_configPanels[i]->m_netCardComb_2->setCurrentText(config_2.netCard);
        emit sigVideoChanged(i, config_2);

        WINDOW_CONFIG config_3 = configs[2 + 4 * i];
        config_3.videoNum = 2;
        m_configPanels[i]->resourcesNameLine_3->setText(config_3.resourcesName);
        m_configPanels[i]->ipLine_3->setText(config_3.ip);
        m_configPanels[i]->portSpinBox_3->setValue(config_3.port);
        m_configPanels[i]->m_netCardComb_3->setCurrentText(config_3.netCard);
        emit sigVideoChanged(i, config_3);

        WINDOW_CONFIG config_4 = configs[3 + 4 * i];
        config_4.videoNum = 3;
        m_configPanels[i]->resourcesNameLine_4->setText(config_4.resourcesName);
        m_configPanels[i]->ipLine_4->setText(config_4.ip);
        m_configPanels[i]->portSpinBox_4->setValue(config_4.port);
        m_configPanels[i]->m_netCardComb_4->setCurrentText(config_4.netCard);
        emit sigVideoChanged(i, config_4);
    }
}

SettingsData SettingFunc::getSettingsData() const
{
    return m_saveSettings->getSettingsData();
}

void SettingFunc::loadVideoMode()
{
    QDir appDir = (QCoreApplication::applicationDirPath());
    QString dir = QDir(appDir).filePath("config/videoMode.ini");
    QSettings settings(dir, QSettings::IniFormat);
    for(int i = 0; i < 4; i++) {
        m_videoMode[i] = settings.value(QString("VideoSettings/videoMode_%1").arg(i), 0).toInt();
    }
}

void SettingFunc::saveVideoMode()
{
    QDir appDir = (QCoreApplication::applicationDirPath());
    QString dir = QDir(appDir).filePath("config/videoMode.ini");
    QSettings settings(dir, QSettings::IniFormat);
    for(int i = 0; i < 4; i++) {
        settings.setValue(QString("VideoSettings/videoMode_%1").arg(i), m_configPanels[i]->m_VideoMode);
    }
    settings.sync();
}

void SettingFunc::changedVideoMode(int displayerID, int index)
{
    if(m_configPanels.size() > displayerID) {
        m_configPanels[displayerID]->changeVideoComb(index);
    }

}

void SettingFunc::showIPInfo(int i, int videoID, QString ipSrc)
{
    m_configPanels[i]->showIPInfo(videoID, ipSrc);
}

void SettingFunc::showSolution(int i, int videoID, int width, int height)
{
    m_configPanels[i]->showSolution(videoID, width, height);
}

void SettingFunc::showFPS(int i, int videoID, int fps)
{
    m_configPanels[i]->showFPS(videoID, fps);
}

void SettingFunc::showEncodeForm(int i, int videoID, QString form)
{
    m_configPanels[i]->showEncodeForm(videoID, form);
}

